package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.common.entity.PageResult;
import com.atguigu.dao.TravelSetmealDao;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.TravelSetmealService;
import com.atguigu.vo.TravelSetmealAddVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = TravelSetmealService.class)
@Transactional
public class TravelSetmealServiceImpl implements TravelSetmealService {

    @Autowired
    TravelSetmealDao travelSetmealDao;
    @Override
    public PageResult selectPage(Integer currpage, Integer pageSize, String queryString) {
        PageHelper.startPage(currpage,pageSize);
        Page<Setmeal> page = travelSetmealDao.selectPage(queryString);
        return new PageResult(page.getTotal(),page.getResult(), (long) page.getPageNum());
    }

    @Override
    public void setmealAdd(TravelSetmealAddVo setmealAddVo) {
        //添加套餐
        Setmeal setmeal = setmealAddVo.getSetmeal();
        travelSetmealDao.setmealAdd(setmeal);
        //添加套餐和旅游团关联表
        List<Integer> ids = setmealAddVo.getIds();
        setmealAndGroupAdd(setmeal, ids);
    }

    private void setmealAndGroupAdd(Setmeal setmeal, List<Integer> ids) {
        for (Integer groupId : ids) {
            travelSetmealDao.setmealAndGroupAdd(setmeal.getId(),groupId);
        }
    }

    @Override
    public void setmealUpdate(Setmeal setmeal) {

    }

    /*
    查询所有
     */
    @Override
    public List<Setmeal> getSemtealAll() {
        return travelSetmealDao.selectAll();
    }

    /*
    根据id查询
     */
    @Override
    public Setmeal findById(Integer id) {
        Setmeal setmeal = travelSetmealDao.findById(id);
        return setmeal;
    }

    @Override
    public List<Integer> selectSetmealIdAndGroup(Integer id) {
        return travelSetmealDao.selectSetmealIdAndGroup(id);
    }
}
